@include "misc.awk"

function args(string,opt,_overrides) {
    args2opt(string,opt)
    overrides(opt,_overrides)
}
function overrides(globals, _str,   pairs,x) {
    if (_str) {
	s2a(_str,pairs,"[=;]")
	for(x in pairs)
	    globals[x] = pairs[x]
}}
function args2opt(string,opt) {
    s2a(string,opt,"[=;]") # e.g. "name=tim;age=100;verbose="
    ARGC = args2opt1(opt,ARGV,ARGC)
}
function args2opt1(opt,input,n,  key,key1,i,j,k,tmp) {
    for(i=1;i<=n;i++)  { # explore args till no more flags
	key = key1 = input[i];
	if (sub(/^[-]+/,"",key))  { # we have a new flag
	    if (key in opt)         # if legal, then change value
		opt[key] = (no(opt[key])) ? 1 : input[++i]  
	    else 
		barph(key1" unknown.")
	} else {
	    i--
	    break
	}}
    for(j=i+1;j<=n;j++) 
	tmp[j-i]=input[j] # copy what's left to tmp
    split("",input,"")   # reset the input args
    for(k in tmp) 
	input[k] = tmp[k] #  move  copy back  args 
    n -= i
    return n
}

